package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 李红磊
 * @since 2021-06-11
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {

        //获取输入流
        try {
            InputStream is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //1查询出所有一级分类 parent_id=0

        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapper);


        //2查询出所有二级分类 parent_id!=0
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapper2);


        //创建一个list，用于存放最终数据
        ArrayList<OneSubject> finalList = new ArrayList<>();

        //3封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值，
        // 封装到要求的list集合里面List<OneSubject> finalList
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);

            OneSubject oneSubject = new OneSubject();
            //oneSubject.setId(eduSubject.getId());
            //oneSubject.setTitle(eduSubject.getTitle());

            BeanUtils.copyProperties(eduSubject,oneSubject);
            //finalList.add(oneSubject);

            //在一级分类循环中遍历所有二级分类
            //创建一个集合来存放每一个一级分类的二级分类
            ArrayList<TwoSubject> twoList = new ArrayList<>();



            for (int m = 0; m < twoSubjectList.size(); m++) {

                EduSubject tWsubject = twoSubjectList.get(m);
                //System.out.println(tWsubject.getParentId()+"====="+eduSubject.getId());
                if(tWsubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    //twoSubject.setId(tWsubject.getId());
                    //twoSubject.setTitle(tWsubject.getTitle());
                    BeanUtils.copyProperties(tWsubject,twoSubject);

                    twoList.add(twoSubject);
                }

            }

            oneSubject.setChildren(twoList);
            finalList.add(oneSubject);

        }


        return finalList;
    }

    @Override
    public List<EduSubject> getLevelTwo(String subjectParentId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",subjectParentId);
        List<EduSubject> list = baseMapper.selectList(wrapper);
        return list;
    }


}
